package Part3_字节流.FileInputStream_FileOutputStream;

import org.junit.Test;

import java.io.*;

/**
 * ClassName: StreamTest
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.04.21 17:17
 */
public class StreamTest {
    @Test
    public void test1(){
        //1、创建File类对象
        File file1 = new File("650.jpg");
        File file2 = new File("copy.jpg");


        //2、创建输入、输出流
        FileInputStream fis = null;
        FileOutputStream fos = null;


        try {

            fis = new FileInputStream(file1);
            fos = new FileOutputStream(file2, true);
            //false-->等同于不写，true-->>追加模式
            //fos = new FileOutputStream(file2, false);
            //fos = new FileOutputStream(file2, true);

            //3、复制操作
            byte[] buffer = new byte[1024];//1KB
            int len;
            while ((len=fis.read(buffer))!=-1){
                fos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis!=null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos!=null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
